package HojaTrabajo3.src.Sorts.Test;

import HojaTrabajo3.MergeSort;
import java.util.Random;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

/**
 * MergeSortTest.java
 *@author Rodrigo 
 * prueba para algoritmo de union
 */


public class MergeSortTest {
    
      public MergeSortTest() {
        
    }

	private Comparable[] numbers;
	private final static int SIZE = 200000;
	private final static int MAX = 20;

	@Before
	public void setUp() throws Exception {
		numbers = new Comparable[SIZE];
		Random generator = new Random();

		for (int i = 0; i < numbers.length; i++) {
			numbers[i] = generator.nextInt(MAX);
		}
	}

	@Test
	public void testMergeSort() {
		long startTime = System.currentTimeMillis();

		MergeSort sorter = new MergeSort();
		sorter.mergesort(numbers);

		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println("Mergesort " + elapsedTime);

		for (int i = 0; i < numbers.length - 1; i++) {
			if ((Integer)numbers[i] > (Integer)numbers[i + 1]) {
				fail("Should not happen");
			}
		}
		assertTrue(true);

	}

	@Test
	@Ignore
	public void itWorksRepeatably() {
		for (int i = 0; i < 200; i++) {
			numbers = new Comparable[SIZE];
			Random generator = new Random();
			for (int a = 0; a < numbers.length; a++) {
				numbers[a] = generator.nextInt(MAX);
			}
			MergeSort sorter = new MergeSort();
			sorter.mergesort(numbers);
			for (int j = 0; j < numbers.length - 1; j++) {
				if ((Integer)numbers[j] > (Integer)numbers[j + 1]) {
					fail("Should not happen");
				}
			}
			assertTrue(true);
		}
	}
}
